package com.lemon.smartsafetynew.ui.adapter.air;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.entity.air.AirItmeVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2018/3/17.
 */

public class AirItemAdapter extends LemonRcvAdapter<AirItmeVo> {

    private Activity activity;

    public AirItemAdapter(@Nullable List<AirItmeVo> data, Activity activity) {
        super(data);
        this.activity = activity;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdpter();
    }

    public class ItemAdpter implements AdapterItem<AirItmeVo> {

        @Bind(R.id.icon)
        ImageView icon;
        @Bind(R.id.ppm_tv)
        TextView ppmTv;
        @Bind(R.id.percent)
        TextView percent;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.deviceID)
        TextView deviceID;
        @Bind(R.id.date_tv)
        TextView dateTv;
        @Bind(R.id.thing_tv)
        TextView thingTv;

        @Override
        public int getLayoutResId() {
            return R.layout.item_air_recycler_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {

        }

        @Override
        public void handleData(AirItmeVo airItmeVo, int position) {

            icon.setBackgroundResource(R.drawable.air_animation);
            AnimationDrawable _animaition = (AnimationDrawable) icon.getBackground();
            _animaition.setOneShot(false);
            _animaition.start();//启动

            ppmTv.setText(CommonUtil.avoidNullString(airItmeVo.getPpm()));
            percent.setText(CommonUtil.avoidNullString(airItmeVo.getPercent()));
            thingTv.setText(CommonUtil.avoidNullString(airItmeVo.getThing()));

            deviceID.setText("位号:" + CommonUtil.avoidNullString(airItmeVo.getNum()));

//            time.setText(CommonUtil.timeToStr2(new Date().getTime()));
//            SimpleDateFormat dateFormat= new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
//            String dateStr = dateFormat.format(new Date());
//            dateTv.setText("更新时间:" + dateStr);

            startGetNetData();

        }


        /**
         * 按频率获取一次数据
         */
        private Timer timer;
        private void startGetNetData() {

            timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            time.setText(CommonUtil.timeToStr2(new Date().getTime()));
                            SimpleDateFormat dateFormat= new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                            String dateStr = dateFormat.format(new Date());
                            dateTv.setText("更新时间:" + dateStr);
                        }
                    });

                }
            }, 0, 1000);
        }
    }
}
